<template>
    <div>
        <top-bar></top-bar>
        <left-bar :datas='data'></left-bar>
        <div class="main">
            <div class="main-mains">
                <div class="main-content">
                    <div class="main-mains-top">
                        医学课堂管理
                    </div>
                    <div class="main-top-nav">
                        <ul>
                            <router-link to="/medicalclassroom">
                                <li>医学课堂列表</li>
                            </router-link>
                            <router-link to="/classroomvideo">
                                <li class="active">课堂视频分类</li>
                            </router-link>
                        </ul>
                    </div>
                    <div class="screen clear">
                        <div class="fl" @click="addProject()">新增</div>
                    </div>
                    <div class="publicTable toLongTable">
                        <table class="head_table">
                            <tr>
                                <td width="5%">ID</td>
                                <td width='7%'>分类名称</td>
                                <td width='8%'>分类图片</td>
                                <td width='7%'>排序</td>
                                <td width='8%'>操作</td>
                            </tr>
                        </table>
                        <el-scrollbar>
                            <table class="content_table">
                                <tr v-for="(item,index) in listData" :key="index" :class="{active:index%2==1}">
                                    <td width="5%">{{item.id}}</td>
                                    <td width='7%'>{{item.name}}</td>
                                    <td width='8%'>
                                        <img :src=item.image_url alt="">
                                    </td>
                                    <td width='7%'>{{item.sort}}</td>
                                    <td width='8%' class="operation">
                                        <span class="active" @click="edit(item)">编辑</span>
                                        <span class="active" @click="removes(item.id)">删除</span>
                                    </td>
                                </tr>
                            </table>
                        </el-scrollbar>
                    </div>
                    <div class="nodataTip" v-if="showNodataTip">
                        <i class="iconfont icon-zanwuneirong"></i>
                        <h5>暂无内容</h5>
                    </div>
                    <div class="orderBotom">
                        <div class="orderBotomRight">
                            <el-pagination @current-change="handleCurrentChange" :page-size="15" layout="total, prev, pager, next" :total='totalnum'>
                            </el-pagination>
                        </div>
                    </div>
                </div>
            </div>
            <div class="system-menban" v-if="editFlag">
            </div>
            <div class="systemPopup" v-if="editFlag">
                <div class="topTitle">
                    <span>编辑</span>
                    <i class="el-icon-close system-icon" @click="hideSystemPopup"></i>
                </div>
                <div class="addBannerPupup">
                    <div class="bannerContent clear">
                        <span class="leftTitle">排序：</span>
                        <div class="rightContent">
                            <el-input clearable v-model="numbers" type="number" placeholder="请输入序号"></el-input>
                        </div>
                    </div>
                    <div class="bannerContent clear">
                        <span class="leftTitle">分类名称：</span>
                        <div class="rightContent">
                            <el-input clearable v-model="name" placeholder="请输入分类名称"></el-input>
                        </div>
                    </div>
                    <div class="bannerContent clear">
                        <span class="leftTitle">选择图片</span>
                        <div class="rightContent">
                            <label class="chooseBanner" for="uploads">选择图片</label>
                            <input type="file" name="img" id="uploads" style="position:absolute; clip:rect(0 0 0 0);" accept="image/png, image/jpeg, image/jpg" @change="uploadImg($event)">
                        </div>
                    </div>
                    <div class="bannerContent clear">
                        <span class="leftTitle">logo</span>
                        <div class="rightContent">
                            <div class="thumbnailBox">
                                <img :src=imgUrl alt="">
                            </div>
                        </div>
                    </div>
                    <div class="system-messagebox1-btn clear">
                        <div @click="addSubmit()"> 确定</div>
                        <div @click="hideSystemPopup()">取消</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>


<script>
import leftBar from "@/components/leftBar";
import topBar from "@/components/topBar";
import pubilc from "@/api/pubilc";
import medicalclassroom from "@/api/medicalclassroom";
export default {
  components: {
    leftBar,
    topBar
  },
  data() {
    return {
      data: "medicalclassroom",
      pages: 1,
      listData: [],
      totalnum: 0,
      showNodataTip: false,
      className: "",
      doctorName: "",
      classType: "",
      classOptions: [],
      editFlag: false,
      numbers: "",
      name: "",
      imgUrl: "",
      imgId: "",
      listId: ""
    };
  },
  methods: {
    edit(data) {
      this.editFlag = true;
      this.name = data.name;
      this.numbers = data.sort;
      this.imgId = data.image_id;
      this.imgUrl = data.image_url;
      this.listId = data.id;
    },
    uploadImg(e) {
      let demos = event.currentTarget.files[0];
      var formdata1 = new FormData();
      formdata1.append("file", demos);
      formdata1.append("type", "7");
      pubilc.uploadImg(formdata1).then(res => {
        if (res.code == 1) {
          this.imgUrl = res.data.file.full_path;
          this.imgId = res.data.file.id;
        }
      });
    },
    addSubmit() {
      if (this.numbers == "") {
        this.$message({
          showClose: true,
          message: "请输入排序！",
          type: "error"
        });
        return false;
      }
      if (this.name == "") {
        this.$message({
          showClose: true,
          message: "请输入分类名称！",
          type: "error"
        });
        return false;
      }
      if (this.imgId == "") {
        this.$message({
          showClose: true,
          message: "请选择logo！",
          type: "error"
        });
        return false;
      }
      const options = {
        id: this.listId,
        name: this.name,
        sort: this.numbers,
        image_id: this.imgId
      };
      medicalclassroom.editCate(options).then(res => {
        if (res.code == 1) {
          this.$message({
            showClose: true,
            message: "修改成功！",
            type: "success"
          });
          this.editFlag = false;
          this.getList();
        }
      });
    },
    addProject() {
      this.$router.push("/addclassroomvideo");
    },
    handleCurrentChange(val) {
      this.pages = val;
      this.getList();
    },
    removes(id) {
      this.$confirm("此操作将删除该删除该分类, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          const options = {
            id: id
          };
          medicalclassroom.delCate(options, id).then(res => {
            if (res.code == 1) {
              this.$message({
                showClose: true,
                message: "删除成功！",
                type: "success"
              });
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    getList() {
      this.showNodataTip = false;
      let options;
      options = {
        page: this.pages
      };
      medicalclassroom.getCateList(options).then(res => {
        if (res.code == 1) {
          this.search = false;
          this.listData = res.data.list;
          this.totalnum = res.data.total;
          if (!res.data.total) {
            this.showNodataTip = true;
          }
        }
      });
    },
    hideSystemPopup() {
      this.editFlag = false;
    }
  },
  mounted() {
    this.getList();
  }
};
</script>

<style>
</style>
